//
//  FGViewController.m
//  FGChatKit-UI
//
//  Created by huangchangweng on 05/08/2023.
//  Copyright (c) 2023 huangchangweng. All rights reserved.
//

#import "FGViewController.h"
#import <FGChatKit/FGChatKit.h>
#import <FGVisitorMessagesViewController.h>
#import <HLTool/MBProgressHUD+HLTool.h>

@interface FGViewController ()<FGChatKitDelegate>
@property (weak, nonatomic) IBOutlet UIStackView *startStackView;
@property (weak, nonatomic) IBOutlet UITextField *idMarkTextField;
@property (weak, nonatomic) IBOutlet UIButton *serviceButton;
@property (weak, nonatomic) IBOutlet UILabel *queueLabel;

@end

@implementation FGViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    [FGChatKit sharedKit].isTest = YES;
    [[FGChatKit sharedKit] addDelegate:self delegateQueue:dispatch_get_main_queue()];
}

- (void)dealloc {
    [[FGChatKit sharedKit] removeDelegate:self];
}

#pragma mark - Private Method

- (void)showAlert:(NSString *)msg
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:^{}];
}

/// 选择主题
- (void)selectTopic
{
    NSArray<FGTopicsModel *> *topicsModels = [FGChatKit sharedKit].topicsModels;
    NSArray *topicNames = [topicsModels valueForKeyPath:@"topicName"];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择主题" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [topicNames enumerateObjectsUsingBlock:^(NSString *topicName, NSUInteger idx, BOOL * _Nonnull stop) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:topicName style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [[FGChatKit sharedKit] selectTopicsForId:topicsModels[idx].id];
            [[FGChatKit sharedKit] enterConversation];
        }];
        [alert addAction:action];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {}];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:^{}];
}

#pragma mark - Response Event

- (IBAction)startAction:(UIButton *)sender {
    NSString *idMark = self.idMarkTextField.text;
    
    if (idMark.length == 0) {
        [MBProgressHUD showError:@"idMark不能为空"];
        return;
    }
    
    [MBProgressHUD show:@"启动中..."];
    [[FGChatKit sharedKit] start:idMark
                       callBlock:^(NSInteger code, NSString *msg, id data) {
        [MBProgressHUD hide];
        if (code == 200) {
            self.startStackView.hidden = YES;
            self.serviceButton.hidden = NO;
            [MBProgressHUD showSuccess:@"启动成功"];
        } else {
            [MBProgressHUD showError:[NSString stringWithFormat:@"启动失败，code:%ld msg:%@", code, msg]];
        }
    }];
}

- (IBAction)serviceAction:(UIButton *)sender {
    NSArray<FGTopicsModel *> *topicsModels = [FGChatKit sharedKit].topicsModels;
    FGTopicsModel *model = [FGChatKit sharedKit].selectedTopicsModel;
    // 已选择主题 或 没有主题列表
    if (model || topicsModels == nil) {
        [MBProgressHUD show:@"进入房间中..."];
        [[FGChatKit sharedKit] enterConversation];
    }
    // 未选择主题
    else {
        [self selectTopic];
    }
}

- (IBAction)logoutAction:(UIButton *)sender {
    [[FGChatKit sharedKit] end];
    self.startStackView.hidden = NO;
    self.serviceButton.hidden = YES;
}

#pragma mark - FGChatKitDelegate

/**
 * 收到进入房间邀请
 * 收到此回调打开会话页面
 */
- (void)chatKitDidReceiveEnterRoomInvitation:(NSString *)roomId
{
    [MBProgressHUD hide];
    [MBProgressHUD showSuccess:@"进入房间成功"];
    
    self.queueLabel.hidden = YES;
    
    FGVisitorMessagesViewController *vc = [[FGVisitorMessagesViewController alloc] init];
    vc.title = [FGChatKit sharedKit].selectedTopicsModel.topicName;
    vc.conversationId = [FGChatKit sharedKit].roomId;
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 * 加入房间失败
 * @param code  错误码
 * @param msg   错误信息
 */
- (void)chatKitEnterRoomFailure:(NSInteger)code msg:(NSString *)msg
{
    [MBProgressHUD hide];
    [MBProgressHUD showError:[NSString stringWithFormat:@"加入房间失败，code:%ld msg:%@", code, msg]];
}

/**
 * 收到排队消息回调
 * @param queueNum 排队人数
 */
- (void)chatKitDidReceiveQueue:(NSInteger)queueNum
{
    if (queueNum == 0) {
        return;
    }
    self.queueLabel.hidden = NO;
    self.queueLabel.text = [NSString stringWithFormat:@"当前排队：%ld", queueNum];
}

@end
